const { Model } = require('sequelize')
const teacherSchema = require('./teacherSchema')
const sequelize = require('../../common/sequelize')


class Teacher extends Model {
  /**
   * @description 传入教师用户的Userid,查询教师表中相关教师的信息，如果查询到就返回对应教师信息，没有则返回null
   * @param {string} id 教师用户的唯一id
   * @returns 包含教师用户信息的数据集
   */
  static async getInfoById(id) {
    const res = (await this.findOne({
      where: { UserId: id },
      attributes: {
        exclude: ['id', 'UserId']
      }
    })).dataValues
    return res ? res : null
  }

  /**
   * @description 查询教师表中的所有数据并返回
   * @returns {Array} 返回一个包含教师表中所有教师信息的数组
   */
  static async getTeachersInfo() {
    const res = await this.findAll({
      attributes: {
        exclude: ['id']
      },
      raw: true
    })
    console.log(res);
    return res
  }
}

Teacher.init(teacherSchema, {
  sequelize,
  timestamps: true,
  updatedAt: false,
  tableName: 'teachers',
  modelName: 'Teacher'
});

module.exports = Teacher

// (async function () { await Teacher.sync() })()